<?php

namespace App\Http\Requests\airline\flight;

use App\Http\ValidateResult;
use Illuminate\Support\Facades\Validator;

trait TransportRequest
{
    public static function add_check(array $params): ValidateResult {
        $validator = Validator::make($params, [
            'take_off_time' => 'required|date_format:Y-m-d H:i:s',
            'landing_time' => 'required|date_format:Y-m-d H:i:s',
            'unload_warehouse_id' => 'required|integer',
//            'volume' => 'required|numeric|min:0',
//            'weight' => 'required|numeric|min:0',
//            "bans" => 'required|array',
            "lines" => 'required|array',
            'carrier' => 'required|string',
            'cust_type' => 'required',
            'jh_type' => 'required',
        ], [
            'take_off_time.required' => '请选择起飞时间',
            'take_off_time.date_format' => '起飞时间格式不正确',
            'landing_time.required' => '请选择降落时间',
            'landing_time.date_format' => '降落时间格式不正确',
            'unload_warehouse_id.required' => '请选择卸货仓库',
            'unload_warehouse_id.integer' => '卸货仓库ID必须是整数',
            'volume.required' => '请输入可运输体积',
            'volume.numeric' => '可运输体积必须是数字',
            'volume.min' => '可运输体积不能为负数',
            'weight.required' => '请输入可运输重量',
            'weight.numeric' => '可运输重量必须是数字',
            'weight.min' => '可运输重量不能为负数',
            'bans.required' => '请添加版型',
            'lines.required' => '请添加航班',
            'carrier.required' => '请输入庄家',
            'cust_type.required' => '请输入报关方式',
            'jh_type.required' => '请输入交货方式',
        ]);

        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }

        $bans = $params['bans'] ?? [];
        foreach($bans as $ban) {
            if(!is_array($ban)) {
                return ValidateResult::error('板型错误');
            }
            $validator = Validator::make($ban, [
                'id' => 'required|integer',
                'nums' => 'required|integer|min:0',
            ], [
                'id.required' => '请选择版型',
                'nums.required' => '请输入板型数量',
                'nums.integer' => '板型数量必须是整数',
                'nums.min' => '板型数量不能为负数',
            ]);
        }

//        $lines = $params['lines'];
//        foreach($lines as $line) {
//            if(!is_array($line)) {
//                return ValidateResult::error('航班错误');
//            }
//            $validator = Validator::make($ban, [
//                'route_id' => 'required|integer',
//                'start_port_id' => 'required|integer',
//                'destination_id' => 'required|integer',
//                'arrive_port_id' => 'required|integer',
//                'airline_company_id' => 'required|integer',
//                'airline_flight_id' => 'required|integer',
//            ], [
//                'route_id.required' => '请选择航线',
//                'start_port_id.required' => '请选择起飞港口',
//                'destination_id.required' => '请选择目的国家',
//                'arrive_port_id.required' => '请选择到达港口',
//                'airline_company_id.required' => '请选择航空公司',
//                'airline_flight_id.required' => '请选择航班'
//            ]);
//        }

        return ValidateResult::correct();
    }

}
